<?php


namespace api\modules\v1\controllers\buss;

use api\controllers\OnAuthController;
use common\helpers\CommonHelper;
use services\buss\DoctorsService;
use services\member\MemberService;
use Yii;

class DoctorsController extends OnAuthController
{
    public $modelClass = '';

    /**
     * @api {get} /v1/buss/doctors/index 医生列表
     * @apiGroup Doctors
     * @apiName index
     * @apiUse CommonParam
     *
     * @apiParam {String} keywords               医生姓名
     * @apiParam {int} project_id                项目ID
     * @apiParam {int} type                      医生类型
     * @apiParam {int} status                    状态
     * @apiParam {int} page
     * @apiParam {int} pageSize
     *
     * @apiSuccess {Object}  data
     * @apiSuccess {Array}   data.list                              列表数据
     * @apiSuccess {Number}  data.list.id                           ID
     * @apiSuccess {Number}  data.list.name                         医生
     * @apiSuccess {String}  data.list.account_num                  账号数
     * @apiSuccess {String}  data.list.pop_stars
     * @apiSuccess {String}  data.list.pop_stars_name               推广等级
     * @apiSuccess {Number}  data.list.hospital_name                医院
     * @apiSuccess {String}  data.list.type                         医生类型
     * @apiSuccess {Number}  data.list.project_id                   项目ID
     * @apiSuccess {String}  data.list.project_name                 项目
     * @apiSuccess {Number}  data.list.sort                         排序
     * @apiSuccess {Number}  data.list.status                       状态
     * @apiSuccess {String}  data.list.status_name                  状态名
     * @apiSuccess {String}  data.list.created_at
     * @apiSuccess {String}  data.list.updated_at
     * @apiSuccess {String}  data.condition
     * @apiSuccess {String}  data.condition.project_arr                     项目筛选
     * @apiSuccess {String}  data.condition.status_arr                      状态
     * @apiSuccess {String}  data.condition.type_arr                        医生类型
     *
     */
    public function actionIndex()
    {
        $params = Yii::$app->request->get();

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $doctorsService = new DoctorsService();
        $list = $doctorsService->doctorsList($params, $page, $pageSize);

        // 筛选项
        $list['condition'] = [
            'project_arr' => MemberService::getProjectListCondition(),
            'status_arr' => DoctorsService::getStatusCondition(),
            'type_arr' => DoctorsService::getTypeCondition(),
        ];
        return $list;
    }

    /**
     * @api {get} /v1/buss/doctors/info     详情数据
     * @apiGroup Doctors
     * @apiName info
     * @apiUse CommonParam
     *
     * @apiParam {int} id                医生ID
     *
     * @apiSuccess {Object}  data
     * @apiSuccess {Number}  id
     * @apiSuccess {Number}  project_id
     * @apiSuccess {Number}  pop_stars
     * @apiSuccess {String}  hospital_name
     * @apiSuccess {Number}  type
     * @apiSuccess {Number}  status
     * @apiSuccess {Array}   data.condition
     * @apiSuccess {Array}   data.condition.project_arr                     项目筛选
     * @apiSuccess {Array}   data.condition.pop_stars_arr                   推广星级
     * @apiSuccess {Array}   data.condition.type_arr                        医生类型
     *
     */
    public function actionInfo(): array
    {
        $params = Yii::$app->request->get();
        $id = $params['id'] ?? 0;

        // 查询结果
        $service = new DoctorsService();
        return $service->getInfo((int)$id);
    }


    /**
     * @api {post} /v1/buss/doctors/update-doctors 更新医生
     * @apiGroup Doctors
     * @apiName update-doctors
     * @apiUse CommonParam
     *
     * @apiBody {Number}   id                                       医生ID【编辑时必传】
     * @apiBody {String}   name                                     医生
     * @apiBody {Number}   pop_stars                                推广星级
     * @apiBody {Number}   hospital_name                            医院名称
     * @apiBody {String}   type                                     医生类型
     * @apiBody {String}   project_id                               项目ID
     * @apiBody {Number}   status                                   状态【-1：删除、0：禁用、1：启用】
     *
     * @apiSuccess {Number} data    新增/编辑ID
     */
    public function actionUpdateDoctors()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['name']) || !$params['name']) {
            return CommonHelper::api(422, '医生必填');
        }
        $params['id'] = $params['id'] ?? 0;
        $doctorsService = new DoctorsService();
        return $doctorsService->updateDoctors($params);
    }

    /**
     * @api {post} /v1/buss/doctors/delete-doctors 删除医生
     * @apiGroup Doctors
     * @apiName delete-doctors
     * @apiUse CommonParam
     *
     * @apiBody {String} id     ID
     *
     * @apiSuccess {Object} data    返回数据
     */
    public function actionDeleteDoctors()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['id']) || !$params['id']) {
            return CommonHelper::api(422, '缺少数据ID');
        }

        $doctorsService = new DoctorsService();
        return $doctorsService->deleteDoctors($params);
    }
}
